package com.itic.ht.cms.studyrecord.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.utils.IdGen;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.ht.cms.studyrecord.persistence.dao.StudyRecordDao;
import com.itic.ht.cms.studyrecord.persistence.model.StudyRecord;
import com.itic.ht.cms.studyrecord.persistence.model.StudyRecordExample;


@Service
@Transactional
public class StudyRecordService {
	
	@Autowired
	private StudyRecordDao studyRecordMapper;
	
	protected static final String SUCCESS = ResponseCode.SUCCESS.getCode();
    protected static final String FAILED = ResponseCode.SERVICE_ERROR.getCode();

	public int count(StudyRecordExample example) {
		return studyRecordMapper.countByExample(example);
	}

	public List<StudyRecord> list(StudyRecordExample example) {
		return studyRecordMapper.selectByExample(example);
	}

	public String add(StudyRecord templet) {
		templet.setDelFlag(Integer.parseInt(DBConst.TRUE));
		templet.setId(IdGen.uuid());
		int code = studyRecordMapper.insert(templet);
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
	}

	/**
     * 根据ID查询指定数据.
     * 
     * @param id
     * @return
     * @throws
     */
    public StudyRecord get(String id) {
        if (StringHelper.isNotEmpty(id)) {
            return studyRecordMapper.selectByPrimaryKey(id);
        }
        return null;
    }
    
    /**
     * 根据ID更新指定数据.
     * 
     * @param model
     * @return
     * @throws
     */
    public String edit(StudyRecord templet) {
        int code = 0;
        String id = templet.getId();
        if (StringHelper.isNotEmpty(id.toString().trim())) {
            if (null == templet.getMtime()) {
            	templet.setMtime(new Date());
            }
            code = studyRecordMapper.updateByPrimaryKeySelective(templet);
        }
        if (1 == code) {
            return SUCCESS;
        } else if (0 == code) {
            return FAILED;
        } else {
            return FAILED;
        }
    }

	/**
     * 根据条件批量删除
     * 
     * @param model
     * @param example
     * @return
     * @throws Exception
     */
    public String deleteByExample(StudyRecord templet, StudyRecordExample example) {
        int code = 0;
        if (null == templet.getMtime()) {
        	templet.setMtime(new Date());
        }
        if (null == templet.getDelFlag() || 0!=templet.getDelFlag()) {
        	templet.setDelFlag(Integer.parseInt(DBConst.FALSE));
        }
        code = studyRecordMapper.updateByExampleSelective(templet, example);
        if (code > 0) {
            return SUCCESS;
        } else {
            return FAILED;
        }
    }
	
}
